﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fashion.ViewModels;
using Fashion.Models;
using Valentica.Libraries;
using System.Data;

namespace Fashion.Controllers
{
    public class WomenController : ThemeController
    {
        int ProductsPerPage = 9;
        string Sex = "Female";
        FashionEntities1 FashionDB = new FashionEntities1();
        //
        // GET: /Women/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List()
        {
            string pageString = "";

            try
            {
                pageString = Request.Url.Segments[3];
                
            }
            catch (Exception)
            {
                pageString = null;
            }

            int page = (String.IsNullOrEmpty(pageString)) ? 1 : Int32.Parse(pageString);
            
            
            StoreIndexViewModel indexModel = new StoreIndexViewModel();

            var categories = indexModel.GetCategoriesBySex(Sex);
            int totalProducts = indexModel.GetTotalProducts(Sex);

            Pagination pagination = new Pagination(true);

            pagination.BaseUrl = "/Women/List/";
            pagination.TotalRows = totalProducts;
            pagination.CurPage = page;
            pagination.PerPage = ProductsPerPage;

            pagination.PrevLink = "Prev";
            pagination.NextLink = "Next";

            pagination.LoadPaginationXml("\\Content\\Pagination.xml");

            string pageLinks = pagination.GetPageLinks();

            int start = (page - 1) * pagination.PerPage;
            int offset = pagination.PerPage;

            List<Product> products = indexModel.GetProducts(start, offset, Sex);

            ViewData["totalProducts"] = totalProducts;
            ViewData["products"] = products;
            ViewData["pageLinks"] = pageLinks;
            
            var viewModel = new StoreIndexViewModel
            {
                Categories = categories.ToList(),
                Products = products
            };

            return View(viewModel);
        }

        public ActionResult Favorite()
        {
            string pageString = "";

            try
            {
                pageString = Request.Url.Segments[3];

            }
            catch (Exception)
            {
                pageString = null;
            }

            int page = (String.IsNullOrEmpty(pageString)) ? 1 : Int32.Parse(pageString);

            Pagination pagination = new Pagination(true);
            pagination.CurPage = page;
            pagination.PerPage = ProductsPerPage;

            StoreIndexViewModel indexModel = new StoreIndexViewModel();

            var categories = from category in FashionDB.Categories
                             select category.Name;

            int start = (page - 1) * pagination.PerPage;
            int offset = pagination.PerPage;

            List<Product> products = indexModel.GetFavoriteProduct(Sex, start,offset);
            int totalProducts = indexModel.totalProducts;

            pagination.BaseUrl = "/Women/Favorite/";
            pagination.TotalRows = totalProducts;

            pagination.PrevLink = "Prev";
            pagination.NextLink = "Next";

            pagination.LoadPaginationXml("\\Content\\Pagination.xml");

            string pageLinks = pagination.GetPageLinks();

            ViewData["totalProducts"] = totalProducts;
            ViewData["products"] = products;
            ViewData["pageLinks"] = pageLinks;

            var viewModel = new StoreIndexViewModel
            {
                Categories = categories.ToList(),
                Products = products
            };

            return View(viewModel);
        }

        public ActionResult Latest()
        {
            string pageString = "";

            try
            {
                pageString = Request.Url.Segments[3];

            }
            catch (Exception)
            {
                pageString = null;
            }

            int page = (String.IsNullOrEmpty(pageString)) ? 1 : Int32.Parse(pageString);

            Pagination pagination = new Pagination(true);
            pagination.CurPage = page;
            pagination.PerPage = ProductsPerPage;

            StoreIndexViewModel indexModel = new StoreIndexViewModel();

            var categories = from category in FashionDB.Categories
                             select category.Name;

            int start = (page - 1) * pagination.PerPage;
            int offset = pagination.PerPage;

            List<Product> products = indexModel.GetLatestProducts(Sex, start, offset);
            int totalProducts = indexModel.totalProducts;

            pagination.BaseUrl = "/Women/Latest/";
            pagination.TotalRows = totalProducts;

            pagination.PrevLink = "Prev";
            pagination.NextLink = "Next";

            pagination.LoadPaginationXml("\\Content\\Pagination.xml");

            string pageLinks = pagination.GetPageLinks();

            ViewData["totalProducts"] = totalProducts;
            ViewData["products"] = products;
            ViewData["pageLinks"] = pageLinks;

            var viewModel = new StoreIndexViewModel
            {
                Categories = categories.ToList(),
                Products = products
            };

            return View(viewModel);
        }

        public ActionResult Rating()
        {
            string pageString = "";

            try
            {
                pageString = Request.Url.Segments[3];

            }
            catch (Exception)
            {
                pageString = null;
            }

            int page = (String.IsNullOrEmpty(pageString)) ? 1 : Int32.Parse(pageString);

            Pagination pagination = new Pagination(true);
            pagination.CurPage = page;
            pagination.PerPage = ProductsPerPage;

            StoreIndexViewModel indexModel = new StoreIndexViewModel();

            var categories = from category in FashionDB.Categories
                             select category.Name;

            int start = (page - 1) * pagination.PerPage;
            int offset = pagination.PerPage;

            List<Product> products = indexModel.GetProductByRating(Sex, start, offset);
            int totalProducts = indexModel.totalProducts;

            pagination.BaseUrl = "/Women/Rating/";
            pagination.TotalRows = totalProducts;

            pagination.PrevLink = "Prev";
            pagination.NextLink = "Next";

            pagination.LoadPaginationXml("\\Content\\Pagination.xml");

            string pageLinks = pagination.GetPageLinks();

            ViewData["totalProducts"] = totalProducts;
            ViewData["products"] = products;
            ViewData["pageLinks"] = pageLinks;

            var viewModel = new StoreIndexViewModel
            {
                Categories = categories.ToList(),
                Products = products
            };

            return View(viewModel);
        }

        //
        // GET: /Women/Browse?Category=Dresses&page=1
        public ActionResult Browse(string category, int page = 1)
        {
            StoreIndexViewModel indexModel = new StoreIndexViewModel();
            int totalProducts = indexModel.CountProductsByCategoryAndSex(category, Sex);

            Pagination pagination = new Pagination(true);

            string link = "/Women/Browse?category=" + category;
            pagination.BaseUrl = link;            
            pagination.TotalRows = totalProducts;
            pagination.CurPage = page;
            pagination.PerPage = ProductsPerPage;

            pagination.PrevLink = "Prev";
            pagination.NextLink = "Next";
            
            pagination.LoadPaginationXml("\\Content\\Pagination.xml");

            string pageLinks = pagination.GetPageLinks();

            int start = (page - 1) * pagination.PerPage;
            int offset = pagination.PerPage;

            //Retrieve list of Categories from database
            
            var categories = indexModel.GetCategoriesBySex(Sex);
            List<Product> products = indexModel.GetProductsByCategoryAndSex(category, Sex, start, offset);
            

            ViewData["totalProducts"] = totalProducts;
            ViewData["products"] = products;
            ViewData["pageLinks"] = pageLinks;

            var viewModel = new StoreIndexViewModel
            {
                Categories = categories.ToList(),
                Products = products
            };

            return View(viewModel);
        }

        //
        // GET: /Women/Product?pid=5&rating=1
        public ActionResult Product(int pid, int? rating)
        {
            var product = FashionDB.Products.Single(p => p.ProductID == pid);

            var productDetail = (from products in FashionDB.ProductDetails
                              where products.ProductID == pid
                              select products).First();

            try
            {
                if (product.Rating == 0)
                    product.Rating = (int)rating;
                else
                    product.Rating = Math.Round((product.Rating + (int)rating) / product.Rating,2);
                FashionDB.SaveChanges();
            }
            catch (Exception)
            {
                rating = null;
            }
            
            var viewMode = new StoreDetailViewModel(pid);

            return View(viewMode);
        }

        //
        // GET: /Women/CategoryMenu
        [ChildActionOnly]
        public ActionResult CategoryMenu()
        {
            var categories = FashionDB.Categories.ToList();

            return View(categories);
        }

        [HttpPost]
        public ActionResult List(FormCollection form)
        {
            string productKeyword = form["ctl00$keyword"];

            return RedirectToAction("Basic", "Search", new { keyword = productKeyword });
        }

        [HttpPost]
        public ActionResult Browse(FormCollection form)
        {
            string productKeyword = form["ctl00$keyword"];

            return RedirectToAction("Basic", "Search", new { keyword = productKeyword });
        }

        [HttpPost]
        public ActionResult Product(FormCollection form)
        {
            string productKeyword = form["ctl00$keyword"];

            return RedirectToAction("Basic", "Search", new { keyword = productKeyword });
        }
    }
}
